using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Kenly.DBFramework.ORMCodeHelper
{
    public partial class frmOption : Form
    {
        public frmOption()
        {
            InitializeComponent();

            ReadSettings();
        }

        #region Settings

        private void ReadSettings()
        {
            string defaultConfigFilePath = PathConst.ConfigFilePath;
            ReadSettings(defaultConfigFilePath);
        }

        private void ReadSettings(string defaultConfigFilePath)
        {
            if (!System.IO.File.Exists(defaultConfigFilePath))
            {
                return;
            }
            UserSettings userSettings = UserSettings.Deserialize(defaultConfigFilePath);
            txtConnectionString.Text = userSettings.ConnectionString;
            if (!string.IsNullOrEmpty(userSettings.DefaultNameSpace))
            {
                txtNameSpace.Text = userSettings.DefaultNameSpace;
            }

            txtCSPath.Text = PathConst.OutputDir;
            
            chkAddAttribute.Checked = userSettings.AddAttribute;
            chkGenerateProject.Checked = userSettings.AddProjectFiles;

            BindTemplates(userSettings);
            BindDataSources(userSettings);
        }

        private void BindTemplates(UserSettings userSettings)
        {
            string[] filePaths = Directory.GetFiles(PathConst.ModelTemplateDir);
            List<string> fileNames = new List<string>();

            foreach (string filePath in filePaths)
            {
                int lastIndex = filePath.LastIndexOf(@"\");
                if (lastIndex > 0 && filePath.Length > lastIndex + 1)
                {
                    fileNames.Add(filePath.Substring(lastIndex + 1));
                }
            }

            cmbTemplateDir.DataSource = fileNames;

            if (cmbTemplateDir.Items.Count > 0)
            {
                cmbTemplateDir.Text = userSettings.SelectedTemplate;
            }
        }

       

        private void SaveSettings(string defaultConfigFilePath)
        {
            if (System.IO.File.Exists(defaultConfigFilePath))
            {
                System.IO.File.Delete(defaultConfigFilePath);
            }
            UserSettings userSettings = new UserSettings();

            userSettings.SelectedDataSource = cmbDataSource.Text;

            userSettings.DefaultNameSpace = txtNameSpace.Text;
            userSettings.OutputDir = PathConst.OutputDir;

            userSettings.SelectedTemplate = cmbTemplateDir.Text;

            userSettings.AddAttribute = chkAddAttribute.Checked;
            userSettings.AddProjectFiles = chkGenerateProject.Checked;
            UserSettings.Serialize(defaultConfigFilePath, userSettings);
        }


        private void BindDataSources(UserSettings userSettings)
        {
            List<DataSource> dataSources = DataSource.Deserialize(PathConst.DataSourceFilePath);
            if (dataSources == null)
            {
                dataSources = new List<DataSource>();
            }

            cmbDataSource.DisplayMember = "Key";
            cmbDataSource.ValueMember = "ConnectionString";
            cmbDataSource.DataSource = dataSources;

            if (cmbDataSource.Items.Count > 0)
            {
                cmbDataSource.Text = userSettings.SelectedDataSource;
            }
        }


        #endregion

        private void btnOk_Click(object sender, EventArgs e)
        {
            SaveToList();

            string defaultConfigFilePath = PathConst.ConfigFilePath;
            SaveSettings(defaultConfigFilePath);
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddToList_Click(object sender, EventArgs e)
        {
            SaveToList();
            ReadSettings();
        }

        private void cmbDataSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtConnectionString.Text = cmbDataSource.SelectedValue != null ?
                cmbDataSource.SelectedValue.ToString() : "";
        }

        private void SaveToList()
        {
            List<DataSource> dataSources = DataSource.Deserialize(PathConst.DataSourceFilePath);
            if (dataSources == null)
            {
                dataSources = new List<DataSource>();
            }

            for (int i = 0; i < dataSources.Count; i++)
            {
                if (dataSources[i].Key == cmbDataSource.Text)
                {
                    dataSources[i].ConnectionString = txtConnectionString.Text;
                    DataSource.Serialize(PathConst.DataSourceFilePath, dataSources.ToArray());
                    return;
                }
            }

            DataSource newDataSource = new DataSource();
            newDataSource.Key = cmbDataSource.Text;
            newDataSource.ConnectionString = txtConnectionString.Text;

            dataSources.Add(newDataSource);

            DataSource.Serialize(PathConst.DataSourceFilePath, dataSources.ToArray());
        }

      

    }
}